
 <?php if($_SESSION['Permit']==1)
 { 
   
	$sqlsection =sprintf("select * from tblsection");
    $sqlmenusection =sprintf("select id,vi_name from tblmenu where  section_id >=0 and parent_id <>0");
 } 
 else
 { 
		 $sqlsection =sprintf("SELECT id,name FROM tblsection where id in(%s)",$_SESSION['ArraySec']);
		 $sqlmenusection =sprintf("select id,vi_name from tblmenu where section_id in (%s)",$_SESSION['ArraySec']);
 }   
  if(isset($_POST['temp']))
  {
  
  $sqlarticle =sprintf("select id,vi_title from tblarticle where section_id=%s",$_POST['temp']);
  $Article=mysql_query($sqlarticle); 
  }
  $sqlmenu =sprintf("select id,vi_name from tblmenu where parent_id='0'");
 
  $Menu=mysql_query($sqlmenu)or die(mysql_error());
  $MenuSection=mysql_query($sqlmenusection)or die(mysql_error());
  $Section=mysql_query($sqlsection) or die(mysql_error());
  

   
?>  
  <script type="text/javascript">
  
function btnAdd_Click(){
	var frm = document.detailt;
	var menus= frm.menus;
	var level;
	var valid = true;
	for(var i = 0; i<menus.length;i++){
		
		if(menus[i].checked){
				level = menus[i].value;
		}
	}
	
	var sid = frm.sid.value;
	var aid = frm.aid.value;
	var vi_name = frm.vi_name.value;
	var en_name = frm.en_name.value;
	
	
	if(level =="section" && sid=="0"){
		alert("Menu cấp 2 phải được liên kết đến 1 chuyên mục!");
		valid = false;
	}
	if(level =="article" && (aid =="0" || sid=="0")){
		alert("Menu cấp 3 phải được liên kết đến 1 bài viết!");
		valid = false;
	}
	if(vi_name==""){
		alert("Thiếu tên tiếng Việt!");
		valid=false;
	}
	if(en_name==""){
		alert("Thiếu tên tiếng Anh!");
		valid=false;
	}
	if(valid){
		frm.submit();
		
	}

}
function test(control)
  {	  
   
    
	   if(document.getElementById('parent_id').value=="")
	   {
	   alert('Xin hãy chọn 1 chuyên mục!');
	   return 0;
	   }
	   document.detailt.submit();
  }

  function submitt(control)
  {	  
   
    document.getElementById('temp').value=control.value;
	
	document.formEdit.submit();
  }
  function changevalue(control)
  {	  
  	var rdMenus = document.getElementsByName("menus");
	var rdChecked;
	 for(var i = 0; i<rdMenus.length;i++){
		  if(rdMenus[i].checked){
			  rdChecked = rdMenus[i];
		  }
	}
  	if(control.value!=0){
		
     	document.getElementById('url').value="/view/"+control.value;
	}
  }
  
  
  </script>
   <form name="formEdit" action="" method="POST" style="visibility:hidden">
   <input type="hidden" name="temp" id="temp" value=""/>
   </form>
<div style="font-size:16px;"> 

     <?php if(isset($_GET['id'])) $row_CurrentLink=mysql_fetch_assoc($Menu);  ?>
  <fieldset style="width:90%;">
       
  <legend>Tạo menu mới</legend>
  <form name="detailt" action="Menu/addmenu.php" method="POST">
     <fieldset style="width:90%;">
       
  <legend>Lựa chọn bài viết cho menu</legend>
     <p id="field"><span  id="text">Chuyên mục</span>
      <select name="sid" onchange="submitt(this)" disable="disable">
      	<option value="0">Lựa chọn chuyên mục</option>
        <?php 
		
while ($rowitem=mysql_fetch_assoc($Section)){  
?>
        <option value="<?php echo $rowitem['id']?>" <?php if(isset($_POST['temp'])&&$_POST['temp']==$rowitem['id']) echo "selected='selected'"; ?> ><?php echo $rowitem['name']?></option>

        <?php		
} 

?>     </select></p>
    <p id="field"><span  id="text" >Link tới bài viết</span>
      <select name="aid" id="link" onchange="changevalue(this)" onfocus="changevalue(this)" disable="disable" >
      	<option value="0">Lựa chọn bài viết</option>
        <?php 
	
while ($rowitem=mysql_fetch_assoc($Article)){  
?>
        <option value="<?php echo $rowitem['id']?>" ><?php echo $rowitem['vi_title']?></option>
        <?php
	
} 
 
?>     </select>
    
    </p>
    </fieldset>
      
      <input name="link_url" id="url" type="hidden" value=""/>
      
        <span  id="text" >Cấp menu</span>
          <p style=" <?php if($_SESSION['Permit']!=1) echo "display:none" ?>">
          	<label for="menus-none">
            	<input  id="menus-none" type="radio" name="menus" value="menu" onclick="disableall();"  checked="checked" onchange="rdMenus_change();"/>
      		Cấp 1
	      </label> 
     	 <label for="menus-all">
         	<input id="menus-all"  type="radio" name="menus" value="section" onclick="enableselections();" onchange="rdMenus_change();" />
            Cấp 2
         </label> 
							
		<label for="menus-select">
        	<input id="menus-select" type="radio" name="menus" value="article" onclick="enablesection();" onchange="rdMenus_change();"/>
            Cấp 3
        </label>
      </p>
           <p id="field"><span id="text">Lựa chọn Menu Cha</span>
            
             <select style=" <?php if($_SESSION['Permit']!=1) echo "display:none" ?>" name="parent_id" id="parent_id" size="6" >
                     <?php 
							
						while ($rowitem=mysql_fetch_assoc($Menu)){  
						?>
								<option value="<?php echo $rowitem['id']?>"><?php echo $rowitem['vi_name']?></option>
								<?php		
						} 
						 
						?> 
             </select>
             <select name="section_id" id="section_id" size="6" >
               <?php 
								
						while ($rowitem=mysql_fetch_assoc($MenuSection)){  
				?>
               	<option value="<?php echo $rowitem['id']?>"><?php echo $rowitem['vi_name']?></option>
               <?php		
				} 
						 
				?>
             </select>
           </p>
   
   
   
    
        
       <input name="menu_id" type="hidden" value="<?php if(isset($_GET['id'])) echo $row_CurrentLink['id']; ?>" />

     
     <p id="field"><span id="text">Tên Tiếng Việt </span> 
         <input name="vi_name" type="text"  value="" size="50" />
      </p>
        <p id="field"><span id="text">Tên Tiếng Anh </span> 
          <input name="en_name" type="text"  value="" size="50" />
        </p>
    
    
     
   
   <p id="field">
       
       <input type="hidden" name="order"  value="0"/>   
      </p>
    <p id="field" align="center">    
       <label for="button">
         <input type="button" name="btnAdd" value="Thêm mới" onclick="btnAdd_Click()"/>
         <input type="reset" value="Làm lại"/>
       </label>
      </p>
  </form>
</fieldset>
<script type="text/javascript"> 
				 
				<?php if($_SESSION['Permit']==1)echo "disableall()";
				else echo "enablesection()";
				 ?>
				  
				  function disableall() {
					  disableselections();
					  disablesection();
				  }
					function disableselections() {
						var e = document.getElementById('parent_id');
							e.disabled = true;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = true;
							e.options[i].selected = false;
						}
					}
					function disablesection() {
						var e = document.getElementById('section_id');
							e.disabled = true;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = true;
							e.options[i].selected = false;
						}
					}
					function enableselections() {
						var e = document.getElementById('parent_id');
						disablesection();
							e.disabled = false;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = false;
						}
						e.options[0].selected = true;
					}
					function enablesection() {
						var e = document.getElementById('section_id');
						 disableselections();
							e.disabled = false;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = false;
						}
						e.options[0].selected = true;
					}
				</script> 
</div>